package com.suray.wcs.service.newled;

import java.util.HashMap;
import java.util.Map;

public class Datagram {

    private Map<String, String> hashMap = new HashMap<>();

    public static Datagram getShowData(String address, String district, String moveWay, String context) {
        Datagram datagram = new Datagram(address, district, null, moveWay, context);
        return datagram;
    }

    private Datagram(String address, String district, String addContent, String moveWay, String context) {
        hashMap.put("起始符0", "!#");
        hashMap.put("设备地址1", address);
        hashMap.put("区域编号2", "%Z" + (district == null ? "01" : district));
        hashMap.put("内容追加3", "%M" + addContent);
        hashMap.put("字号4", "%F" + "16");
        hashMap.put("水平对齐方式5", "%AH" + "0");
        hashMap.put("垂直对齐方式6", "%AV" + "0");
        hashMap.put("动画方式7", "%DA" + (moveWay == null ? "01" : moveWay));
        hashMap.put("动画速度8", "%DS" + "10");
        hashMap.put("动画停留9", "%DH" + "10");
        hashMap.put("文字字段10", "%" + "1");
        hashMap.put("文本内容11", context);
        hashMap.put("结束符12", "$$");
    }

    public String getShowDatagram() {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < hashMap.size(); i++) {
            for (Map.Entry<String, String> entry : hashMap.entrySet()) {
                if (getMapIndex(entry.getKey()) == i) {
                    if (entry.getValue().contains("null")) {
                        continue;
                    }
                    stringBuffer.append(entry.getValue());
                }
            }
        }
        return stringBuffer.toString();
    }

    private int getMapIndex(String key) {
        String indexStr = "";
        for (int i = 0; i < key.length(); i++) {
            try {
                Integer.valueOf(key.charAt(i) + "");
                indexStr += key.charAt(i);
            } catch (NumberFormatException e) {
                continue;
            }
        }
        return Integer.valueOf(indexStr);
    }
}
